<template>
  <div class="cityS_body">
    <div class="city_list">
      <div class="city_hot">
        <h2>热门城市</h2>
        <ul class="clearfix">
          <li
            v-for="item in hotList"
            :key="item.id"
            @click="handelToCity(item.nm,item.id)"
          >{{item.nm}}</li>
        </ul>
      </div>
      <div class="city_sort" ref="city_sort">
        <div v-for="item in cityList" :key="item.index">
          <h2>{{item.index}}</h2>
          <ul>
            <li
              v-for="itemList in item.list"
              :key="itemList.id"
              @click="handelToCity(itemList.nm,itemList.id)"
            >{{ itemList.nm }}</li>
          </ul>
        </div>
      </div>
    </div>
    <div class="city_index">
      <ul>
        <!-- <li v-for="item in cityList" :key="item.index">{{item.index}}</li> -->
        <li
          v-for="(item,index) in cityList"
          :key="item.index"
          @touchstart="handleToIndex(index)"
        >{{ item.index }}</li>
      </ul>
    </div>
    <!-- <div class="city_list">
              <Loading v-if="isLoading" />
              <Scroller v-else ref="city_List">
                  <div>
                      <div class="city_hot">
                          <h2>热门城市</h2>
                          <ul class="clearfix">
                              <li v-for="item in hotList" :key="item.id" @tap="handleToCity(item.nm , item.id)">{{ item.nm }}</li>
                          </ul>
                      </div>
                      <div class="city_sort" ref="city_sort">
                          <div v-for="item in cityList" :key="item.index">
                              <h2>{{ item.index }}</h2>
                              <ul>
                                  <li v-for="itemList in item.list" :key="itemList.id" @tap="handleToCity(itemList.nm , itemList.id)">{{ itemList.nm }}</li>
                              </ul>
                          </div>	
                      </div>
                  </div>
              </Scroller>
          </div>
          <div class="city_index">
              <ul>
                  <li v-for="(item,index) in cityList" :key="item.index" @touchstart="handleToIndex(index)">{{ item.index }}</li>
              </ul>
    </div>-->
  </div>
</template>

<script>
import scroll from "../scroll/scroll";
import { hostname } from "os";
export default {
  name: "city",
  data() {
    return {
      cityList: [],
      hotList: []
    };
  },
  components: {
    scroll
  },
  mounted() {
    const cityList = window.localStorage.getItem("cityList");
    const hotList = window.localStorage.getItem("hotList");

    if (cityList && hotList) {
      this.cityList = JSON.parse(cityList);
      this.hotList = JSON.parse(hotList);
    }

    this.axios.get("/api/cityList").then(res => {
      // console.log(res.data.data.cities)
      const msg = res.data.msg;
      if (msg === "ok") {
        const cities = res.data.data.cities;
        const { cityList, hotList } = this.formatCityList(cities);
        this.cityList = cityList;
        this.hotList = hotList;
        window.localStorage.setItem("cityList", JSON.stringify(cityList));
        window.localStorage.setItem("hotList", JSON.stringify(hotList));
      }
    });
  },
  methods: {
    formatCityList(cities) {
      let cityList = [];
      let hotList = [];

      for (let i = 0; i < cities.length; i++) {
        if (cities[i].isHot === 1) {
          hotList.push(cities[i]);
        }
      }

      for (let i = 0; i < cities.length; i++) {
        const firstLetter = cities[i].py.substring(0, 1).toUpperCase();
        if (toCom(firstLetter)) {
          cityList.push({
            index: firstLetter,
            list: [{ nm: cities[i].nm, id: cities[i].id }]
          });
        } else {
          for (var j = 0; j < cityList.length; j++) {
            if (cityList[j].index === firstLetter) {
              cityList[j].list.push({ nm: cities[i].nm, id: cities[i].id });
            }
          }
        }
      }
      cityList.sort((n1, n2) => {
        if (n1.index < n2.index) {
          return -1;
        } else if (n1.index > n2.index) {
          return 1;
        } else {
          return 0;
        }
      });

      function toCom(firstLetter) {
        for (var i = 0; i < cityList.length; i++) {
          if (cityList[i].index === firstLetter) {
            return false;
          }
        }
        return true;
      }
      return {
        cityList,
        hotList
      };
    },
    handleToIndex(index) {
      var h2 = this.$refs.city_sort.getElementsByTagName("h2");
      // this.$refs.city_sort.parentNode.scrollTop = h2[index].offsetTop;
      // this.$refs.city_List.toScrollTop(-h2[index].offsetTop);
      this.$refs.city_sort.parentNode.scrollTop = h2[index].offsetTop;
      // console.log(h2)
    },
    handelToCity(nm, id) {
      this.$store.commit("city/CITY_INFO", { nm, id });
      window.localStorage.setItem("nowNm", nm);
      window.localStorage.setItem("nowId", id);
      this.$router.push("/movie/nowplaing");
    }
  }
};
</script>

<style scoped>
.cityS_body {
  margin-top: 5px;
  display: flex;
  width: 100%;
  /* position: relative; */
  top: 0;
  bottom: 0;
}
.city_list {
  flex: 1;
  overflow: auto;
  background: #fff5f0;
}
.city_list::-webkit-scrollbar {
  background-color: transparent;
  width: 0;
}
.city_hot {
  margin-top: 20px;
}
.city_hot h2 {
  padding-left: 15px;
  line-height: 30px;
  font-size: 14px;
  background: #f0f0f0;
  font-weight: normal;
}
.city_hot ul li {
  float: left;
  background: #fff;
  width: 29%;
  height: 33px;
  margin-top: 15px;
  margin-left: 3%;
  padding: 0 4px;
  border: 1px solid #e6e6e6;
  border-radius: 3px;
  line-height: 33px;
  text-align: center;
  box-sizing: border-box;
}
.city_sort div {
  margin-top: 20px;
}
.city_sort h2 {
  padding-left: 15px;
  line-height: 30px;
  font-size: 14px;
  background: #f0f0f0;
  font-weight: normal;
}
.city_sort ul {
  padding-left: 10px;
  margin-top: 10px;
}
.city_sort ul li {
  line-height: 30px;
  line-height: 30px;
}
.city_index {
  width: 20px;
  background: #fff;
  height: 63%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  /* position: relative; */
  /* position: absolute; */
  position: fixed;
  right: 0;
  top: 16.5%;
  /* padding-top: 20%; */
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  /* justify-content: center; */
  text-align: center;
  border-left: 1px #e6e6e6 solid;
}
</style>
